package com.dounine.clouddisk360.pool;

import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import org.apache.http.HttpClientConnection;
import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;

public class PoolingHttpClientConnection {

	private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = null;
	static{
		
		try {
			SSLContextBuilder builder = SSLContexts.custom();
			builder.loadTrustMaterial(null, new TrustStrategy() {
				@Override
				public boolean isTrusted(X509Certificate[] chain, String authType)
						throws java.security.cert.CertificateException {
					return true;
				}
			});
			
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier(){
				@Override
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
			Registry<ConnectionSocketFactory> socketFactoryRegistry = 
					  RegistryBuilder.<ConnectionSocketFactory> create()
					  .register("https", sslsf)
					  .register("http", new PlainConnectionSocketFactory()).build();
			poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
		} catch (Exception e) {
		}
		
		poolingHttpClientConnectionManager.setMaxTotal(200);//设置最大连接数
		poolingHttpClientConnectionManager.setDefaultMaxPerRoute(20);//设置每个路由默认连接数
		
		HttpRoute yunpanRoute = new HttpRoute(new HttpHost("yunpan.360.cn", 80));
		
		// 获取新的连接. 这里可能耗费很多时间  
		ConnectionRequest connRequest = poolingHttpClientConnectionManager.requestConnection(yunpanRoute, null);
		
		// 10秒超时  
		try {
			HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
			if(!conn.isOpen()){
				poolingHttpClientConnectionManager.connect(conn, yunpanRoute, 1000, new HttpClientContext());//httpClientContext在些初始化有问题
			}
		} catch (ConnectionPoolTimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		new IdleConnectionMonitorThread(poolingHttpClientConnectionManager).start();//连接回收策略
	}
	
	public static PoolingHttpClientConnectionManager getInstalce(){
		return poolingHttpClientConnectionManager;
	}
	
}
